//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imagemanager.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Process;
import android.provider.MediaStore.Video.Thumbnails;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import org.apache.http.conn.ClientConnectionManager;

public final class MediaItemTexture extends Texture {
    public static final int MAX_FACES = 1;
    private static final String TAG = "MediaItemTexture";
    private static final int CACHE_HEADER_SIZE = 12;
    private final MediaItemTexture.Config mConfig;
    private final MediaItem mItem;
    private Context mContext;
    private boolean mIsRetrying;
    private boolean mCached;

    public MediaItemTexture(Context context, MediaItemTexture.Config config, MediaItem item) {
        this.mConfig = config;
        this.mContext = context;
        this.mItem = item;
        this.mCached = this.computeCache();
    }

    private boolean computeCache() {
        MediaItemTexture.Config config = this.mConfig;
        MediaItem item = this.mItem;
        DiskCache cache = null;
        MediaSet parentMediaSet = item.mParentMediaSet;
        if(config != null && parentMediaSet != null) {
            cache = CacheService.sThumbnailCache;
            if(cache == CacheService.sThumbnailCache && item.mMimeType != null && item.mMimeType.contains("video")) {
                cache = CacheService.sThumbnailCacheVideo;
            }
        }

        if(cache == null) {
            return false;
        } else {
            synchronized(cache) {
                long id = parentMediaSet.mPicasaAlbumId == -1L?Utils.Crc64Long(item.mFilePath):item.mId;
                return cache.isDataAvailable(id, item.mDateModifiedInSec * 1000L);
            }
        }
    }

    public boolean isUncachedVideo() {
        return this.isCached()?false:(this.mItem.mParentMediaSet != null && this.mItem.mMimeType != null?this.mItem.mParentMediaSet.mPicasaAlbumId == -1L && this.mItem.mMimeType.contains("video"):false);
    }

    public boolean isCached() {
        return this.mCached;
    }

    public Bitmap load() {
        MediaItemTexture.Config config = this.mConfig;
        MediaItem item = this.mItem;
        String uriString = item.mContentUri;
        Uri data;
        if(uriString != null) {
            data = Uri.parse(uriString);
            if(data.getScheme().equals("content") && !data.getAuthority().equals("media")) {
                try {
                    return UriTexture.createFromUri(this.mContext, item.mThumbnailUri, 128, 128, 0L, (ClientConnectionManager)null);
                } catch (IOException var10) {
                    return null;
                } catch (URISyntaxException var11) {
                    return null;
                }
            }
        }

        MediaSet cache;
        if(config == null) {
            Bitmap retVal = null;

            try {
                if(this.mItem.getMediaType() == 0) {
                    Process.setThreadPriority(0);

                    try {
                        cache = null;
                        MediaSet parentMediaSet = item.mParentMediaSet;
                        if(parentMediaSet != null) {
                            DiskCache cache1 = CacheService.sThumbnailCache;
                            if(cache1 == CacheService.sThumbnailCache) {
                                if(item.mMimeType != null && item.mMimeType.contains("video")) {
                                    cache1 = CacheService.sThumbnailCacheVideo;
                                }

                                long crc64 = Utils.Crc64Long(item.mFilePath);
                                if(!cache1.isDataAvailable(crc64, item.mDateModifiedInSec * 1000L)) {
                                    UriTexture.invalidateCache(crc64, 1024);
                                }
                            }
                        }

                        retVal = UriTexture.createFromUri(this.mContext, this.mItem.mContentUri, 1024, 1024, Utils.Crc64Long(item.mFilePath), (ClientConnectionManager)null);
                    } catch (IOException var13) {
                        ;
                    } catch (URISyntaxException var14) {
                        ;
                    }

                    Process.setThreadPriority(10);
                } else {
                    Process.setThreadPriority(0);
                    (new Thread() {
                        public void run() {
                            try {
                                Thread.sleep(5000L);
                            } catch (InterruptedException var3) {
                                ;
                            }

                            try {
                                Thumbnails.cancelThumbnailRequest(MediaItemTexture.this.mContext.getContentResolver(), MediaItemTexture.this.mItem.mId);
                            } catch (Exception var2) {
                                ;
                            }

                        }
                    }).start();
                    retVal = Thumbnails.getThumbnail(this.mContext.getContentResolver(), this.mItem.mId, 1, (Options)null);
                    Process.setThreadPriority(10);
                }
            } catch (OutOfMemoryError var15) {
                Log.i("MediaItemTexture", "Bitmap creation fail, outofmemory");

                try {
                    if(!this.mIsRetrying) {
                        Thread.sleep(1000L);
                        this.mIsRetrying = true;
                    }
                } catch (InterruptedException var12) {
                    ;
                }
            }

            return retVal;
        } else {
            data = null;
            cache = item.mParentMediaSet;
            byte[] data1;
            if(cache != null && cache.mPicasaAlbumId != -1L) {
                DiskCache thumbnailCache = CacheService.sThumbnailCache;
                data1 = thumbnailCache.get(item.mId, 0L);
                if(data1 == null) {
                    try {
                        Bitmap retVal = UriTexture.createFromUri(this.mContext, item.mThumbnailUri, 256, 256, 0L, (ClientConnectionManager)null);
                        data1 = CacheService.writeBitmapToCache(thumbnailCache, item.mId, item.mId, retVal, config.thumbnailWidth, config.thumbnailHeight, item.mDateModifiedInSec * 1000L);
                    } catch (IOException var16) {
                        return null;
                    } catch (URISyntaxException var17) {
                        return null;
                    }
                }
            } else {
                data1 = CacheService.queryThumbnail(this.mContext, Utils.Crc64Long(item.mFilePath), item.mId, item.getMediaType() == 1, item.mDateModifiedInSec * 1000L);
            }

            if(data1 != null) {
                try {
                    ByteArrayInputStream cacheInput = new ByteArrayInputStream(data1);
                    DataInputStream dataInput = new DataInputStream(cacheInput);
                    item.mThumbnailId = dataInput.readLong();
                    item.mThumbnailFocusX = dataInput.readShort();
                    item.mThumbnailFocusY = dataInput.readShort();
                    Options options = new Options();
                    options.inDither = false;
                    options.inScaled = false;
                    options.inPreferredConfig = android.graphics.Bitmap.Config.RGB_565;
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data1, 12, data1.length - 12, options);
                    return bitmap;
                } catch (IOException var18) {
                    ;
                }
            }

            return null;
        }
    }

    public static final class Config {
        public int thumbnailWidth;
        public int thumbnailHeight;

        public Config() {
        }
    }
}
